home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / aspisrc.zip / GETDATE.C < prev    next >
C/C++ Source or Header  |  1992-01-26  |  39KB  |  1,508 lines

  1.  
  2. # line 2 "getdate.y"
  3. /* $Revision: 2.1 $
  4. **
  5. **  Originally written by Steven M. Bellovin <smb@research.att.com> while
  6. **  at the University of North Carolina at Chapel Hill.  Later tweaked by
  7. **  a couple of people on Usenet.  Completely overhauled by Rich $alz
  8. **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
  9. **  send any email to Rich.
  10. **
  11. **  This grammar has eight shift/reduce conflicts.
  12. **
  13. **  This code is in the public domain and has no copyright.
  14. */
  15. /* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
  16. /* SUPPRESS 288 on yyerrlab *//* Label unused */
  17.  
  18. #ifdef __GNUC__
  19. #define alloca __builtin_alloca
  20. #else
  21. #ifdef sparc
  22. #include <alloca.h>
  23. #else
  24. #ifdef _AIX /* for Bison */
  25. #pragma alloca
  26. #else
  27. char *alloca ();
  28. #endif
  29. #endif
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #include <ctype.h>
  34.  
  35. #if    defined(vms)
  36. #include <types.h>
  37. #include <time.h>
  38. #else
  39. #include <sys/types.h>
  40. #if    defined(USG)
  41. /*
  42. **  Uncomment the next line if you need to do a tzset() call to set the
  43. **  timezone, and don't have ftime().  Some SystemV releases, I think.
  44. */
  45. /*#define NEED_TZSET */
  46. struct timeb {
  47.     time_t        time;        /* Seconds since the epoch    */
  48.     unsigned short    millitm;    /* Field not used        */
  49.     short        timezone;
  50.     short        dstflag;    /* Field not used        */
  51. };
  52. #else
  53. #include <sys/timeb.h>
  54. #endif    /* defined(USG) */
  55. #if    defined(BSD4_2) || defined(BSD4_1C)
  56. #include <sys/time.h>
  57. #else
  58. #include <time.h>
  59. #endif    /* defined(BSD4_2) */
  60. #endif    /* defined(vms) */
  61.  
  62. #if defined (STDC_HEADERS) || defined (USG)
  63. #include <string.h>
  64. #endif
  65.  
  66. extern struct tm    *localtime();
  67.  
  68. #define yyparse getdate_yyparse
  69. #define yylex getdate_yylex
  70. #define yyerror getdate_yyerror
  71.  
  72. #if    !defined(lint) && !defined(SABER)
  73. static char RCS[] =
  74.     "$Header: str2date.y,v 2.1 90/09/06 08:15:06 cronan Exp $";
  75. #endif    /* !defined(lint) && !defined(SABER) */
  76.  
  77.  
  78. #define EPOCH        1970
  79. #define HOUR(x)        (x * 60)
  80. #define SECSPERDAY    (24L * 60L * 60L)
  81.  
  82.  
  83. /*
  84. **  An entry in the lexical lookup table.
  85. */
  86. typedef struct _TABLE {
  87.     char    *name;
  88.     int        type;
  89.     time_t    value;
  90. } TABLE;
  91.  
  92.  
  93. /*
  94. **  Daylight-savings mode:  on, off, or not yet known.
  95. */
  96. typedef enum _DSTMODE {
  97.     DSTon, DSToff, DSTmaybe
  98. } DSTMODE;
  99.  
  100. /*
  101. **  Meridian:  am, pm, or 24-hour style.
  102. */
  103. typedef enum _MERIDIAN {
  104.     MERam, MERpm, MER24
  105. } MERIDIAN;
  106.  
  107.  
  108. /*
  109. **  Global variables.  We could get rid of most of these by using a good
  110. **  union as the yacc stack.  (This routine was originally written before
  111. **  yacc had the %union construct.)  Maybe someday; right now we only use
  112. **  the %union very rarely.
  113. */
  114. static char    *yyInput;
  115. static DSTMODE    yyDSTmode;
  116. static time_t    yyDayOrdinal;
  117. static time_t    yyDayNumber;
  118. static int    yyHaveDate;
  119. static int    yyHaveDay;
  120. static int    yyHaveRel;
  121. static int    yyHaveTime;
  122. static int    yyHaveZone;
  123. static time_t    yyTimezone;
  124. static time_t    yyDay;
  125. static time_t    yyHour;
  126. static time_t    yyMinutes;
  127. static time_t    yyMonth;
  128. static time_t    yySeconds;
  129. static time_t    yyYear;
  130. static MERIDIAN    yyMeridian;
  131. static time_t    yyRelMonth;
  132. static time_t    yyRelSeconds;
  133.  
  134.  
  135. # line 135 "getdate.y"
  136. typedef union  {
  137.     time_t        Number;
  138.     enum _MERIDIAN    Meridian;
  139. } YYSTYPE;
  140. # define tAGO 257
  141. # define tDAY 258
  142. # define tDAYZONE 259
  143. # define tID 260
  144. # define tMERIDIAN 261
  145. # define tMINUTE_UNIT 262
  146. # define tMONTH 263
  147. # define tMONTH_UNIT 264
  148. # define tSEC_UNIT 265
  149. # define tSNUMBER 266
  150. # define tUNUMBER 267
  151. # define tZONE 268
  152. #define yyclearin yychar = -1
  153. #define yyerrok yyerrflag = 0
  154. extern int yychar;
  155. extern int yyerrflag;
  156. #ifndef YYMAXDEPTH
  157. #define YYMAXDEPTH 150
  158. #endif
  159. YYSTYPE yylval, yyval;
  160. typedef int yytabelem;
  161. # define YYERRCODE 256
  162.  
  163. # line 331 "getdate.y"
  164.  
  165.  
  166. /* Month and day table. */
  167. static TABLE    MonthDayTable[] = {
  168.     { "january",    tMONTH,  1 },
  169.     { "february",    tMONTH,  2 },
  170.     { "march",        tMONTH,  3 },
  171.     { "april",        tMONTH,  4 },
  172.     { "may",        tMONTH,  5 },
  173.     { "june",        tMONTH,  6 },
  174.     { "july",        tMONTH,  7 },
  175.     { "august",        tMONTH,  8 },
  176.     { "september",    tMONTH,  9 },
  177.     { "sept",        tMONTH,  9 },
  178.     { "october",    tMONTH, 10 },
  179.     { "november",    tMONTH, 11 },
  180.     { "december",    tMONTH, 12 },
  181.     { "sunday",        tDAY, 0 },
  182.     { "monday",        tDAY, 1 },
  183.     { "tuesday",    tDAY, 2 },
  184.     { "tues",        tDAY, 2 },
  185.     { "wednesday",    tDAY, 3 },
  186.     { "wednes",        tDAY, 3 },
  187.     { "thursday",    tDAY, 4 },
  188.     { "thur",        tDAY, 4 },
  189.     { "thurs",        tDAY, 4 },
  190.     { "friday",        tDAY, 5 },
  191.     { "saturday",    tDAY, 6 },
  192.     { NULL }
  193. };
  194.  
  195. /* Time units table. */
  196. static TABLE    UnitsTable[] = {
  197.     { "year",        tMONTH_UNIT,    12 },
  198.     { "month",        tMONTH_UNIT,    1 },
  199.     { "fortnight",    tMINUTE_UNIT,    14 * 24 * 60 },
  200.     { "week",        tMINUTE_UNIT,    7 * 24 * 60 },
  201.     { "day",        tMINUTE_UNIT,    1 * 24 * 60 },
  202.     { "hour",        tMINUTE_UNIT,    60 },
  203.     { "minute",        tMINUTE_UNIT,    1 },
  204.     { "min",        tMINUTE_UNIT,    1 },
  205.     { "second",        tSEC_UNIT,    1 },
  206.     { "sec",        tSEC_UNIT,    1 },
  207.     { NULL }
  208. };
  209.  
  210. /* Assorted relative-time words. */
  211. static TABLE    OtherTable[] = {
  212.     { "tomorrow",    tMINUTE_UNIT,    1 * 24 * 60 },
  213.     { "yesterday",    tMINUTE_UNIT,    -1 * 24 * 60 },
  214.     { "today",        tMINUTE_UNIT,    0 },
  215.     { "now",        tMINUTE_UNIT,    0 },
  216.     { "last",        tUNUMBER,    -1 },
  217.     { "this",        tMINUTE_UNIT,    0 },
  218.     { "next",        tUNUMBER,    2 },
  219.     { "first",        tUNUMBER,    1 },
  220. /*  { "second",        tUNUMBER,    2 }, */
  221.     { "third",        tUNUMBER,    3 },
  222.     { "fourth",        tUNUMBER,    4 },
  223.     { "fifth",        tUNUMBER,    5 },
  224.     { "sixth",        tUNUMBER,    6 },
  225.     { "seventh",    tUNUMBER,    7 },
  226.     { "eighth",        tUNUMBER,    8 },
  227.     { "ninth",        tUNUMBER,    9 },
  228.     { "tenth",        tUNUMBER,    10 },
  229.     { "eleventh",    tUNUMBER,    11 },
  230.     { "twelfth",    tUNUMBER,    12 },
  231.     { "ago",        tAGO,    1 },
  232.     { NULL }
  233. };
  234.  
  235. /* The timezone table. */
  236. static TABLE    TimezoneTable[] = {
  237.     { "gmt",    tZONE,     HOUR( 0) },    /* Greenwich Mean */
  238.     { "ut",    tZONE,     HOUR( 0) },    /* Universal (Coordinated) */
  239.     { "utc",    tZONE,     HOUR( 0) },
  240.     { "wet",    tZONE,     HOUR( 0) },    /* Western European */
  241.     { "bst",    tDAYZONE,  HOUR( 0) },    /* British Summer */
  242.     { "wat",    tZONE,     HOUR( 1) },    /* West Africa */
  243.     { "at",    tZONE,     HOUR( 2) },    /* Azores */
  244. #if    0
  245.     /* For completeness.  BST is also British Summer, and GST is
  246.      * also Guam Standard. */
  247.     { "bst",    tZONE,     HOUR( 3) },    /* Brazil Standard */
  248.     { "gst",    tZONE,     HOUR( 3) },    /* Greenland Standard */
  249. #endif
  250.     { "nft",    tZONE,     HOUR(3.5) },    /* Newfoundland */
  251.     { "nst",    tZONE,     HOUR(3.5) },    /* Newfoundland Standard */
  252.     { "ndt",    tDAYZONE,  HOUR(3.5) },    /* Newfoundland Daylight */
  253.     { "ast",    tZONE,     HOUR( 4) },    /* Atlantic Standard */
  254.     { "adt",    tDAYZONE,  HOUR( 4) },    /* Atlantic Daylight */
  255.     { "est",    tZONE,     HOUR( 5) },    /* Eastern Standard */
  256.     { "edt",    tDAYZONE,  HOUR( 5) },    /* Eastern Daylight */
  257.     { "cst",    tZONE,     HOUR( 6) },    /* Central Standard */
  258.     { "cdt",    tDAYZONE,  HOUR( 6) },    /* Central Daylight */
  259.     { "mst",    tZONE,     HOUR( 7) },    /* Mountain Standard */
  260.     { "mdt",    tDAYZONE,  HOUR( 7) },    /* Mountain Daylight */
  261.     { "pst",    tZONE,     HOUR( 8) },    /* Pacific Standard */
  262.     { "pdt",    tDAYZONE,  HOUR( 8) },    /* Pacific Daylight */
  263.     { "yst",    tZONE,     HOUR( 9) },    /* Yukon Standard */
  264.     { "ydt",    tDAYZONE,  HOUR( 9) },    /* Yukon Daylight */
  265.     { "hst",    tZONE,     HOUR(10) },    /* Hawaii Standard */
  266.     { "hdt",    tDAYZONE,  HOUR(10) },    /* Hawaii Daylight */
  267.     { "cat",    tZONE,     HOUR(10) },    /* Central Alaska */
  268.     { "ahst",    tZONE,     HOUR(10) },    /* Alaska-Hawaii Standard */
  269.     { "nt",    tZONE,     HOUR(11) },    /* Nome */
  270.     { "idlw",    tZONE,     HOUR(12) },    /* International Date Line West */
  271.     { "cet",    tZONE,     -HOUR(1) },    /* Central European */
  272.     { "met",    tZONE,     -HOUR(1) },    /* Middle European */
  273.     { "mewt",    tZONE,     -HOUR(1) },    /* Middle European Winter */
  274.     { "mest",    tDAYZONE,  -HOUR(1) },    /* Middle European Summer */
  275.     { "swt",    tZONE,     -HOUR(1) },    /* Swedish Winter */
  276.     { "sst",    tDAYZONE,  -HOUR(1) },    /* Swedish Summer */
  277.     { "fwt",    tZONE,     -HOUR(1) },    /* French Winter */
  278.     { "